﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Net;
using System.Net.Mail;
using System.Web.Mvc;

namespace rosata.Controllers
{
    public class ContattiController : Controller
    {
        //
        // GET: /Contatti/
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult SendMail(SendMailDto sendMailDto)
        {
            try
            {
                MailMessage mailMsg = new MailMessage
                {
                    IsBodyHtml = true,
                    From = new MailAddress("noreply@rosataantennisti.it")
                };

                mailMsg.Subject = "Contatto da sito internet" + sendMailDto.Oggetto;
                mailMsg.To.Add("skyservice.rimini@gmail.com");
                mailMsg.Body = "nome: " + sendMailDto.Nome + "<br/>";
                mailMsg.Body += "email: " + sendMailDto.Email + "<br/>";
                mailMsg.Body += "messaggio: " + sendMailDto.Messaggio;
                mailMsg.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient("rosataantennisti.it");
                NetworkCredential credential = new NetworkCredential("noreply@rosataantennisti.it", "mu66:o::");
                smtp.Credentials = credential;
                //smtp.EnableSsl = true;
                //smtp.UseDefaultCredentials = false;

                //e Procedo con l'invio
                smtp.Send(mailMsg);
            }
            catch (System.Exception exception)
            {
                ModelState.AddModelError("ERRORE", exception);
                return PartialView("OutputSendMail", "ore: " + DateTime.Now.ToString() + "invio email fallita, errore: " + exception.Message);
            }

            return PartialView("OutputSendMail", "ore: " + DateTime.Now.ToString() + " Email inviata correttamente!");
        }

        public class SendMailDto
        {
            [Required(AllowEmptyStrings = false, ErrorMessage = "inserire nome")]
            public string Nome { get; set; }

            [Required(AllowEmptyStrings = false, ErrorMessage = "inserire oggetto")]
            public string Oggetto { get; set; }

            [Required(AllowEmptyStrings = false, ErrorMessage = "inserire email")]
            public string Email { get; set; }

            [Required(AllowEmptyStrings = false, ErrorMessage = "inserire messaggio")]
            public string Messaggio { get; set; }
        }

    }
}
